#include "LoginScene.h "
#include "ui/CocosGUI.h"
#include "Scene/UserChangeScene.h"
#include "AudioEngine.h"
// using namespace cocos2d::experimental;
using namespace ui;
USING_NS_CC;
int LoginScene::login_BGM = 1;
Scene* LoginScene::createScene () {
    auto login_scene = LoginScene::create();
    return login_scene;
}
// on "init" you need to initialize your instance
bool LoginScene::init () {
    if (!Scene::init()) {
        return false;
    }
    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    // 播放背景音乐
    AudioEngine::preload("Music/mainmusic.ogg");
    login_BGM = AudioEngine::play2d("Music/mainmusic.ogg", true);
    
    // 创建背景并缩放到合适大小
    auto back_ground = Sprite::create("Games/Login/GameLoad.png");
    back_ground->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
    this->addChild(back_ground, 0);

    // 进度条 读取到100%
    // 草皮下面的土条
    auto down_bar = Sprite::create("Games/Login/down.png");
    down_bar->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 5));
    this->addChild(down_bar, 1);
    // 草皮
    auto grass = Sprite::create("Games/Login/grass.png");
    grass->setPosition(Vec2(visibleSize.width / 2, visibleSize.height/5 + 29));
    this->addChild(grass, 1);
    // 滚动草皮
    auto scroll_grass = Sprite::create("Games/Login/scrollgrass.png");
    scroll_grass->setPosition(Vec2(visibleSize.width/2 - down_bar->getContentSize().width/2, visibleSize.height / 5 + 40));
    this->addChild(scroll_grass, 2);
    auto scale = ScaleTo::create(2.6f, 0.5f); // 缩小
    auto move = MoveBy::create(2.6f, Vec2(grass->getContentSize().width-20, -10)); // 移动
    auto rotate = RotateBy::create(2.6f, 720.0f); // 转两圈
    auto spawn = Spawn::create(scale, move, rotate, nullptr); // 三个动作同时发生，产生进度条效果
    scroll_grass->runAction(spawn);
     
    // 跳转到 Main MainMenu 场景
    auto jump_start = MenuItemLabel::create(Label::createWithTTF("click to start!", "TypeFace/Marker Felt.ttf", 20), CC_CALLBACK_1(LoginScene::jumpStartCallback, this));
    auto jump = Menu::create(jump_start, NULL);
    jump->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 5));
    this->addChild(jump, 1);
   
    return true;
}

void LoginScene::jumpStartCallback (cocos2d::Ref* pSender) {
    // 创建注册界面并跳转
    auto scene = UserChangeScene::createScene();
    Director::getInstance()->replaceScene(scene);
}